package top.action;


import org.aspectj.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import top.bean.Goods;
import top.service.GoodsServies;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

@Controller
@RequestMapping("hello")

public class UserImp {
   @Autowired
   private GoodsServies goodsServies;
    @RequestMapping("/test")




    public String test(@RequestBody Goods goods) throws Exception {
//        Goods goods=new Goods();
      goodsServies.creatGoods(goods);
//        User user=new User();
//        user.setName("xingyu");
//        user.setAge("23");
//       System.out.println(user.getAge());
//        modelMap.addAttribute("user",user);

        return "user";
    }

    @RequestMapping("/upload")
    public String upload(MultipartFile multipartFile, HttpServletRequest httpServletRequest) throws IOException {
        String name = multipartFile.getOriginalFilename();
        File file = new File("/" + name);
        multipartFile.transferTo(file);
        return "user";
    }

    @RequestMapping("/down")
    public ResponseEntity<byte[]> down() throws IOException {
        String path = "D:/1.png";
        File file = new File(path);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentDispositionFormData("attachment", path);
        headers.setContentType(MediaType.IMAGE_PNG);
        return new ResponseEntity<byte[]>(FileUtil.readAsByteArray(file),headers, HttpStatus.CREATED);
    }
}

